package org.article;

import processing.core.*;

public class TargetArticle extends Article{
	// Indicator: when 2 sections of this document are selected, this flag is 'on'
	private boolean twoSectionsSelected;
	
	public TargetArticle(float aWidth, float aHeight, PApplet aParent, PGraphics aPgParent, PGraphics aPgPreview) {
		super(aWidth, aHeight, aParent, aPgParent, aPgPreview);
		
		int numberOfSections = this.articleSections.size();
		Section tempSection = null;
		
		for(int i=0; i<numberOfSections; i++){
			tempSection = (Section)this.articleSections.get(i);
			
			tempSection.setMovable(false);
		}
	}
	
	
	/**
	 * DRAW Method.
	 * It is mandatory to set the position of the document BEFORE using this method
	 * @param pgDocuments 
	 * @param pgDocuments 
	 */
	public void draw(){
		super.draw();
		
		drawSectionsFromTarget();
		
		highlightParentSectionsIfMouseOver();
	}
	
	/**
	 * Method that highlights a parent section if there is on transcludedSection witha  mouse over
	 */
	private void highlightParentSectionsIfMouseOver() {
		int numberOfSections = this.articleSections.size();
		Section tempSection = null;
		
		Section mouseOverSection = null;

		for(int i=0; i<numberOfSections; i++){
			tempSection = (Section)this.articleSections.get(i);
			
			if(tempSection.checkForMouseOver()){
				if(tempSection.isTranscluded()){
					mouseOverSection = tempSection;
				}
			}
		}
		
		if(mouseOverSection != null){
			mouseOverSection.getParentSection().forceHighlight(true);			
		}
		else{
			for(int i=0; i<numberOfSections; i++){
				tempSection = (Section)this.articleSections.get(i);

				if(tempSection.isTranscluded()){
					tempSection.getParentSection().forceHighlight(false);
				}
			}
		}
	}


	/**
	 * Method that adds some functionalities to the drawSections method in Article.
	 */
	private void drawSectionsFromTarget(){
		int numberOfSections = this.articleSections.size();
		Section tempSection;
		
		if(inTransclusion){
			int counter;
			
			// If is a target doc, the sections must react to the size of the rectangle
			for(int i=0; i<numberOfSections; i++){
				tempSection = (Section)articleSections.get(i);
				
				tempSection.setRectangleSensitivity(true, articleDrawingPadding);
				
				// TODO: Check when 2 sections are selected and change the rectangle color
				counter = this.getNumberOfSelectedSections();
				
				if(counter > 1){
					twoSectionsSelected = true;
				}
				else{
					twoSectionsSelected = false;
				}
			}

			
		}
	}
	

	/**
	 * This method sets the color for all sections in the document
	 * @param newColor
	 */
	public void setSectionsColor(int newColor) {
		int numberOfSections = articleSections.size();
		Section tempSection;
		
		for (int i=0; i<numberOfSections; i++){
			tempSection = (Section)articleSections.get(i);
			
			tempSection.setSectionColor(newColor);
		}		
	}
	
	public boolean hasTwoSectionsSelected(){
		return this.twoSectionsSelected;
	}

	public int getSelectedSectionsPosition(){
		int numberOfSections = articleSections.size();
		boolean isOver = false;
		Section tempSection;
		
		int position = 0;
		
		for (int i=0; i<numberOfSections; i++){
			tempSection = (Section)articleSections.get(i);
			
			isOver = tempSection.checkForMouseOver();
			
			if(isOver) {
				position = i; 
			}
		}
		
		return position;
	}
	
	
		
	private int getNumberOfSelectedSections(){
		int numberOfSections = articleSections.size();
		
		int sectionCounter = 0;
		
		Section tempSection;
		
		for (int i=0; i<numberOfSections; i++){
			tempSection = (Section)articleSections.get(i);
			
			if(tempSection.checkForMouseOver()){
				sectionCounter++ ;
			}
		}
		
		return sectionCounter;
	}
	
	/**
	 * Method that adds a section in the specified position (TARGET)
	 * @param newSection
	 * @param position
	 */
	public void addNewSection(Section newSection, Section parent, int position){
		newSection.setTranscluded(true, parent);
		
		articleSections.add(position, newSection);
	}

	public void resetStatus() {
		int numberOfSections = articleSections.size();
		
		Section tempSection;
		
		for (int i=0; i<numberOfSections; i++){
			tempSection = (Section)articleSections.get(i);

			tempSection.setRectangleSensitivity(false, articleDrawingPadding);
		}
	}
	
	public void removeTranscludedSection(){
		int numberOfSections = articleSections.size();
		
		Section tempSection;
		
		for (int i=0; i<numberOfSections; i++){
			tempSection = (Section)articleSections.get(i);

			if(tempSection.checkForMouseOver()){
				if(tempSection.isTranscluded()){
					this.articleSections.remove(tempSection);
					
					i = numberOfSections;
				}
			}
		}
	}
}